package cn.com.infohold.smartcity.activity.Chengbaofang;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import cn.com.infohold.smartcity.R;
import cn.com.infohold.smartcity.adapter.Chengbaofang_Adapter;
import cn.com.infohold.smartcity.api.HttpEvent;
import cn.com.infohold.smartcity.api.SDK;
import cn.com.infohold.smartcity.api.bean.ChengBaoFangBean;
import cn.com.infohold.smartcity.api.bean.ContractorRelativesListResult;
import cn.com.infohold.smartcity.api.bean.LandResult;
import cn.com.infohold.smartcity.api.bean.ListResult;
import cn.com.infohold.smartcity.api.bean.RelativeBean;
import cn.com.infohold.smartcity.parent.ParentActivity;
import cn.com.infohold.smartcity.utils.GsonUtil;
import cn.com.infohold.smartcity.utils.RecyclerViewDivider;
import cn.com.infohold.smartcity.utils.RequestCode;
import common.annotation.ViewInject;
import common.dialog.ProgressDialog;
import library.utils.DisplayUtil;

/**
 * Created by Monsoon on 2017/11/8.
 */
public class ChengBaoFangDetail_Activity extends ParentActivity{

    @ViewInject(R.id.ib_back)
    ImageButton ib_back;
    @ViewInject(R.id.tv_code)
    TextView tv_code;
    @ViewInject(R.id.tv_name)
    TextView tv_name;
    @ViewInject(R.id.tv_cardid)
    TextView tv_cardid;
    @ViewInject(R.id.tv_sex)
    TextView tv_sex;
    @ViewInject(R.id.tv_contractortype)
    TextView tv_contractortype;
    @ViewInject(R.id.tv_birth)
    TextView tv_birth;
    @ViewInject(R.id.tv_tel)
    TextView tv_tel;
    @ViewInject(R.id.tv_zipcode)
    TextView tv_zipcode;
    @ViewInject(R.id.tv_address)
    TextView tv_address;

    @ViewInject(R.id.rv_list)
    RecyclerView mRecyclerview;

    private RelativeAdapter mAdapter;
    private ChengBaoFangBean chengBaoFangBean;
    private List<RelativeBean> mList = new ArrayList<>();

    @Override
    protected Object inflateView() {
        return R.layout.activity_chengbaofang_detail;
    }

    @Override
    protected void initView(View anchor) {
        Intent intent = getIntent();
        Bundle cb_bundle = intent.getBundleExtra("cb_bundle");
        if (cb_bundle != null){
            chengBaoFangBean = (ChengBaoFangBean) cb_bundle.getSerializable("cb_detail");
            tv_code.setText(chengBaoFangBean.getCode());
            tv_name.setText(chengBaoFangBean.getName());
            tv_cardid.setText(chengBaoFangBean.getCardid());
            tv_sex.setText(chengBaoFangBean.getSex());
            switch (chengBaoFangBean.getContractortype()){
                case 1:
                    tv_contractortype.setText("个人");
                    break;
                case 2:
                    tv_contractortype.setText("农户");
                    break;
                case 3:
                    tv_contractortype.setText("组织单位");
                    break;
                default:
                    break;
            }
            tv_birth.setText(chengBaoFangBean.getBirth());
            tv_tel.setText(chengBaoFangBean.getTel());
            tv_zipcode.setText(chengBaoFangBean.getZipcode());
            tv_address.setText(chengBaoFangBean.getAddress());

            initData();
        }

        ib_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransitionFade();
            }
        });

    }

    private void initRV(){
        int dividerHeight = DisplayUtil.dp2px(0);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerview.addItemDecoration(new RecyclerViewDivider(dividerHeight));
        mAdapter = new RelativeAdapter(mList);
        mRecyclerview.setAdapter(mAdapter);
    }

    private void initData(){
        SDK.api().getRelativesList(chengBaoFangBean.getId()).postEvent(RequestCode.REQUEST_CODE_GET_RELATIVE_LIST);
        ProgressDialog.show(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getData(HttpEvent<String> httpEvent){

        int requestCode = httpEvent.getRequestCode();
        if (requestCode != RequestCode.REQUEST_CODE_GET_RELATIVE_LIST) {
            ProgressDialog.dismiss();
            return;
        }

        if (httpEvent.getState() != HttpEvent.STATE_SUCCESS) {
            ProgressDialog.dismiss();
            return;
        } else {
            switch (requestCode){
                case RequestCode.REQUEST_CODE_GET_RELATIVE_LIST:
                    Type type = new TypeToken<LandResult<ContractorRelativesListResult<RelativeBean>>>(){}.getType();
                    LandResult<ContractorRelativesListResult<RelativeBean>> result = GsonUtil.fromJson(httpEvent.getData(),type);
                    if (result.getStatus().equals("1")){
                        List<RelativeBean> list = result.getResult().getContractorRelativesList();
                        mList.clear();
                        mList.addAll(list);
                        initRV();
                    }

                    break;
                default:
                    break;
            }
        }
    }

    class RelativeAdapter extends RecyclerView.Adapter<RelativeAdapter.mViewHolder>{

        private List<RelativeBean> list;

        public RelativeAdapter(List<RelativeBean> list) {
            this.list = list;
        }

        @Override
        public mViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            mViewHolder viewHolder = new mViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_relativelist, parent, false));
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(mViewHolder holder, int position) {
            RelativeBean bean = list.get(position);
            holder.tv_name.setText(bean.getName());
            switch (bean.getRelationtype()){
                case 1:
                    holder.tv_relationtype.setText("配偶");
                    break;
                case 2:
                    holder.tv_relationtype.setText("父母");
                    break;
                case 3:
                    holder.tv_relationtype.setText("子女");
                    break;
                case 4:
                    holder.tv_relationtype.setText("兄弟姐妹");
                    break;
                default:
                    break;
            }
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class mViewHolder extends RecyclerView.ViewHolder{

            TextView tv_name;
            TextView tv_relationtype;

            public mViewHolder(View itemView) {
                super(itemView);
                tv_name = (TextView) itemView.findViewById(R.id.tv_name);
                tv_relationtype = (TextView) itemView.findViewById(R.id.tv_relationtype);
            }
        }
    }

}
